package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.core.annotation.Order;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    void insert(Orders orders);
    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);
    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    OrderVO getById(Long id);

    Page<OrderVO> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 修改订单状态
     * @param id
     * */
    @Update("update orders set status = #{status} where id = #{id}")
    void updateStatusById(Long id, Integer status);

    Page<OrderVO> pageQueryByCondition(OrdersPageQueryDTO ordersPageQueryDTO);


    /**
     * 商家取消订单
     * @param
     */
    @Update("update orders set status = #{status}, cancel_reason = #{ordersCancelDTO.cancelReason} where id = #{ordersCancelDTO.id}")
    void updateStatusByCancel(@Param("ordersCancelDTO") OrdersCancelDTO ordersCancelDTO, @Param("status") Integer status);
    @Update("update orders set status = #{status}, rejection_reason = #{ordersRejectionDTO.rejectionReason} where id = #{ordersRejectionDTO.id}")
    void updateStatusByReject(@Param("ordersRejectionDTO") OrdersRejectionDTO ordersRejectionDTO, @Param("status") Integer status);

    OrderOverViewVO statistics();
    /**
     * 根据订单状态和下单时间查询订单
     * @param status
     * @param orderTime
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> getByStatusAndOrderTime(Integer status, LocalDateTime orderTime);

   /**
     * 根据动态条件查询营业额
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    /**
     * 根据动态条件查询订单记录数
     * @param map
     * @return
     */
    Integer countByMap(Map map);

    /**
     * 查询指定时间区间内的销量排名top10
     * @param begin
     * @param end
     * @return
     */
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);
}
